package my.facebook;

import java.util.List;
import com.restfb.DefaultLegacyFacebookClient;
import com.restfb.LegacyFacebookClient;
import com.restfb.Parameter;
import com.restfb.types.Group;
import com.restfb.types.User;


public class GraphExample {
    private LegacyFacebookClient facebookClient;
    private Long uid;

    public GraphExample(String accessToken){
        facebookClient = new DefaultLegacyFacebookClient(accessToken);
        uid = facebookClient.execute("users.getLoggedInUser", Long.class);
    }


    public List<Group> fqlGroups() {
        //String query = "SELECT uid2 FROM friend WHERE uid1=" + uid;
        String query = "SELECT description FROM group WHERE gid IN"
                + "(SELECT gid FROM group_member WHERE uid=" + uid +")";
        List<Group> groups =
            facebookClient.executeForList("fql.query", Group.class, Parameter.with("query", query),
            Parameter.with("return_ssl_resources", "true"));
        return groups;
    }

    public String fqlUserName() {
        //String query = "SELECT uid2 FROM friend WHERE uid1=" + uid;
        String query = "SELECT name FROM user WHERE uid=" + uid;
        List<User> userName =
            facebookClient.executeForList("fql.query", User.class, Parameter.with("query", query),
            Parameter.with("return_ssl_resources", "true"));
        return userName.get(0).getName();
    }

    public Long getUserId(){
        return uid;
    }
}